package com.htudata.zabbix.base.service;

import com.htudata.core.util.operation.StringOperationUtils;
import org.springframework.stereotype.Service;

@Service
public class ZabbixErrorService {

  public static String errorToZH(String error) {
    String result = hostErrorToZH(error);
    if (StringOperationUtils.isValidStr(result))
      return result; 
    result = templateErrorToZH(error);
    if (StringOperationUtils.isValidStr(result))
      return result; 
    result = itemErrorToZH(error);
    if (StringOperationUtils.isValidStr(result))
      return result; 
    result = triggerErrorToZH(error);
    if (StringOperationUtils.isValidStr(result))
      return result; 
    result = baseErrorToZH(error);
    if (StringOperationUtils.isValidStr(result))
      return result; 
    return error;
  }
  
  public static String hostErrorToZH(String error) {
    if (error.startsWith("Host") && error.endsWith("already exists.")) {
      String result = error;
      result = result.replace("Host", "对象节点");
      result = result.replace("with the same visible name", "可见名称：");
      result = result.replace("already exists.", "已经存在。");
      return result;
    } 
    return "";
  }
  
  public static String templateErrorToZH(String error) {
    if (error.startsWith("Template") && error.endsWith("already exists.")) {
      String result = error;
      result = result.replace("Template", "监控组件");
      result = result.replace("with the same visible name", "可见名称：");
      result = result.replace("already exists.", "已经存在。");
      return result;
    } 
    if (error
      .equals("Template cannot be linked to another template more than once even through other templates."))
      return "如果一个监控组件已经继承了其他监控组件，那么该监控组件将无法被其他监控组件所继承。请重新选择。"; 
    if (error.equals("Circular template linkage is not allowed."))
      return "监控组件无法继承自身，请重新选择。"; 
    return "";
  }
  
  public static String itemErrorToZH(String error) {
    if (error.startsWith("Item with key") && error.contains("already exists on")) {
      String result = error;
      result = result.replace("Item with key", "监控指标键");
      result = result.replace("already exists on", "已经存在于该对象节点");
      result = result.substring(0, result.indexOf("已经存在于该对象节点") + "已经存在于该对象节点".length()) + "。";
      return result;
    } 
    if (error.startsWith("Item") && error.contains("already exists on") && error
      .contains("inherited from another template")) {
      String result = error;
      result = result.replace("Item", "增加的监控组件含有的监控指标");
      result = result.replace("already exists on", "已经存在于该对象节点对应主机");
      result = result.replace(", inherited from another template.", "，该监控指标继承自其它监控组件。");
      result = result.substring(0, result.length() - 1) + "。";
      return result;
    } 
    if (error.startsWith("Discovery rule") && error.contains("already exists on") && error
      .contains("inherited from another template")) {
      String result = error.substring(error.indexOf("already exists on"));
      result = "增加的监控组件含有的监控指标" + result;
      result = result.replace("already exists on", "已经存在于该对象节点对应主机");
      result = result.replace(", inherited from another template.", "，该监控指标继承自其它监控组件。");
      result = result.substring(0, result.length() - 1) + "。";
      return result;
    } 
    if (error.startsWith("Graph") && error.contains("already exists on") && error
      .contains(" (items are not identical).")) {
      String result = error.substring(error.indexOf("already exists on"));
      result = "增加的监控组件含有的监控指标" + result;
      result = result.replace("already exists on", "已经存在于该对象节点对应主机");
      result = result.replace(" (items are not identical).", "。");
      return result;
    } 
    if (error.startsWith("Incorrect value for field") && error.contains("a time unit is expected.")) {
      String result = error;
      result = result.replace("Incorrect value for field", "无效的字段值。");
      result = result.replace("history", "历史数据存储周期");
      result = result.replace("trends", "趋势数据存储周期");
      result = result.replace("a time unit is expected.", "时间单位有误。");
      return result;
    } 
    if (error.startsWith("Incorrect value for field") && error.endsWith("invalid delay.")) {
      String result = error;
      result = result.replace("Incorrect value for field", "无效的字段值。");
      result = result.replace("invalid delay.", "应该在1s到1d之间。");
      result = result.replace("delay", "更新间隔");
      return result;
    } 
    if (error.equals("Cannot delete templated item."))
      return "无法直接删除通过链接监控组件得来的监控指标，只能在创建该监控指标的监控组件中删除。"; 
    if (error.equals("Item uses host interface from non-parent host."))
      return "系统内部错误：监控指标所使用的主机接口没有对应的主机。"; 
    return "";
  }
  
  public static String triggerErrorToZH(String error) {
    if (error.startsWith("Trigger") && error.contains("already exists on")) {
      String result = error;
      result = result.replace("Trigger", "触发器");
      result = result.replace("already exists on", "已经存在于该对象节点");
      result = result.substring(0, result.indexOf("已经存在于该对象节点") + "已经存在于该对象节点".length()) + "。";
      return result;
    } 
    if (error.startsWith("Incorrect trigger expression.") && error
      .contains("does not exist or you have no access to this host.")) {
      String result = error;
      result = result.replace("Incorrect trigger expression.", "无效的触发器表达式。");
      result = result.replace("Host", "对象节点/监控组件");
      result = result.replace("does not exist or you have no access to this host.", "不存在，或者您暂无操作该资源的权限。");
      return result;
    } 
    if (error.startsWith("Incorrect trigger expression. Check expression part starting from \"")) {
      String result = error;
      result = result.replace("Incorrect trigger expression.", "无效的触发器表达式。");
      result = result.replace("Check expression part starting from", "请检查：");
      return result;
    } 
    if (error.equals("Cannot close problem: trigger does not allow manual closing."))
      return "无法关闭：对应的触发器没有设置为允许手动关闭。"; 
    return "";
  }
  
  public static String baseErrorToZH(String error) {
    if (error.contains("already exists"))
      return "您操作的资源已经存在。"; 
    if (error.equals("No permissions to referred object or it does not exist!"))
      return "您暂无操作该资源的权限，或者该资源不存在。"; 
    if (error.equals("You do not have permission to perform this operation."))
      return "您暂无操作该资源的权限。"; 
    if (error.equals("Incorrect arguments passed to function."))
      return "系统内部错误：传递的参数不正确。"; 
    return "";
  }

}
